package com.hzeport.bgs22emduty.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzeport.bgs22emduty.config.login.UserInfoDetailVO;
import com.hzeport.bgs22emduty.dto.ScreenShouldKnowAnswerDTO;
import com.hzeport.bgs22emduty.dto.ScreenShouldKnowDTO;
import com.hzeport.bgs22emduty.entity.ScreenCheckQuestions;
import com.hzeport.bgs22emduty.entity.ScreenShouldKnowAnswer;
import com.hzeport.bgs22emduty.enums.DeleteFlagEnum;
import com.hzeport.bgs22emduty.outbound.ScreenShouldKnowAnswerRepository;
import com.hzeport.bgs22emduty.outbound.ScreenShouldKnowRepository;
import com.hzeport.bgs22emduty.util.FilterHTML;
import com.hzeport.bgs22emduty.vo.ScreenShouldKnow.ScreenShouldKnowSaveVO;
import com.hzeport.bgs22emduty.common.base.BaseEntity;
import com.hzeport.bgs22emduty.common.base.BaseResult;
import com.hzeport.bgs22emduty.common.base.BaseServiceImpl;
import com.hzeport.bgs22emduty.entity.ScreenShouldKnow;
import com.hzeport.bgs22emduty.outbound.db.ScreenShouldKnowRepositoryImpl;
import com.hzeport.bgs22emduty.service.ScreenShouldKnowService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * ${comments}
 *
 * @author code generator
 * @date 2023-02-14 09:39:44
 */
@Service
@Slf4j
public class ScreenShouldKnowServiceImpl extends BaseServiceImpl<ScreenShouldKnowRepositoryImpl, ScreenShouldKnow> implements ScreenShouldKnowService {

    @Resource
    private ScreenShouldKnowRepository screenShouldKnowRepository;
    @Resource
    private ScreenShouldKnowAnswerRepository screenShouldKnowAnswerRepository;

    @Override
    public BaseResult<Page<ScreenShouldKnow>> list(ScreenShouldKnowDTO dto, UserInfoDetailVO userInfoDetailVO) {
        LambdaQueryWrapper<ScreenShouldKnow> queryWrapper=new LambdaQueryWrapper<>();
        if(!CollectionUtils.isEmpty(dto.getData())&&dto.getData().get(0)!=null){
            queryWrapper.ge(ScreenShouldKnow::getCreateTime, LocalDateTime.parse(dto.getData().get(0)+"T00:00:00"));
        }
        if(!CollectionUtils.isEmpty(dto.getData())&&dto.getData().get(1)!=null){
            queryWrapper.le(ScreenShouldKnow::getCreateTime, LocalDateTime.parse(dto.getData().get(1)+"T23:59:59"));
        }
        if(!StringUtils.isEmpty(dto.getQuestions())){
            queryWrapper.like(ScreenShouldKnow::getMainBody,dto.getQuestions())
            .or(lamber->lamber.like(ScreenShouldKnow::getAnswerDesc,dto.getQuestions()));
        }
        queryWrapper.eq(StringUtils.isNotBlank(userInfoDetailVO.getCustomscode()), ScreenShouldKnow::getCustomId,userInfoDetailVO.getCustomscode());
        queryWrapper.orderByDesc(BaseEntity::getCreateTime);
        Page<ScreenShouldKnow> page = new Page<>(dto.getPageNum(),dto.getPageSize());
        Page<ScreenShouldKnow> standardPage = screenShouldKnowRepository.page(page, queryWrapper);
        standardPage.getRecords().forEach(i->{
            i.setMainBody(FilterHTML.getContent(i.getMainBody()));
        });
        return BaseResult.ok(standardPage,"查询成功");
    }

    @Override
    public BaseResult delete(String id, UserInfoDetailVO userInfoDetailVO) {
        screenShouldKnowRepository.update(new LambdaUpdateWrapper<ScreenShouldKnow>()
                .eq(ScreenShouldKnow::getId,id)
                .set(ScreenShouldKnow::getDelFlag, DeleteFlagEnum.IS_DELETE.getValue())
                .set(ScreenShouldKnow::getUpdateBy,userInfoDetailVO.getUserId())
                .set(ScreenShouldKnow::getUpdateTime, LocalDateTime.now()));
        return BaseResult.ok(null,"删除成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResult saveData(ScreenShouldKnowSaveVO screenShouldKnowSaveVO, UserInfoDetailVO userInfoDetailVO) throws Exception {

        if(StringUtils.isEmpty(screenShouldKnowSaveVO.getMainBody())){
            throw new Exception("正文不能为空");
        }
        ScreenShouldKnow screenShouldKnow = new ScreenShouldKnow();
        BeanUtils.copyProperties(screenShouldKnowSaveVO,screenShouldKnow);
        if(StringUtils.isEmpty(screenShouldKnowSaveVO.getId())){
            screenShouldKnow.setCustomId(userInfoDetailVO.getCustomscode());
            screenShouldKnow.setCustomName(userInfoDetailVO.getCustomsName());
            screenShouldKnow.setCreatePerson(userInfoDetailVO.getUserName());
            screenShouldKnow.setCreateBy(userInfoDetailVO.getUserId());
            screenShouldKnow.setCreateTime(LocalDateTime.now());
            screenShouldKnow.setUpdateBy(userInfoDetailVO.getUserId());
            screenShouldKnow.setUpdateTime(LocalDateTime.now());
            screenShouldKnow.setDelFlag(DeleteFlagEnum.NOT_DELETE.getValue());
            screenShouldKnowRepository.save(screenShouldKnow);
        }else {
            ScreenShouldKnow byId = screenShouldKnowRepository.getById(screenShouldKnowSaveVO.getId());
            if (byId != null){
                screenShouldKnow.setRecVersion(byId.getRecVersion());
            }
            screenShouldKnow.setCustomId(userInfoDetailVO.getCustomscode());
            screenShouldKnow.setCustomName(userInfoDetailVO.getCustomsName());
            screenShouldKnow.setUpdateBy(userInfoDetailVO.getUserId());
            screenShouldKnow.setUpdateTime(LocalDateTime.now());
            screenShouldKnow.setCreatePerson(userInfoDetailVO.getUserName());
            screenShouldKnow.setDelFlag(DeleteFlagEnum.NOT_DELETE.getValue());
            screenShouldKnowRepository.updateById(screenShouldKnow);
        }
        return BaseResult.ok(screenShouldKnow,"保存成功");
    }


    @Override
    public BaseResult<ScreenShouldKnow> detail(String id) {
        ScreenShouldKnow byId = screenShouldKnowRepository.getById(id);
        return BaseResult.ok(byId,"查看成功");
    }

    @Override
    public BaseResult answerList(ScreenShouldKnowAnswerDTO dto) throws Exception {
        if (StringUtils.isEmpty(dto.getQuestionId())){
            throw new Exception("题目id不能为空");
        }
        LambdaQueryWrapper<ScreenShouldKnowAnswer> queryWrapper=new LambdaQueryWrapper<>();
        if(!StringUtils.isEmpty(dto.getQuestionId())){
            queryWrapper.eq(ScreenShouldKnowAnswer::getQuestionId,dto.getQuestionId());
        }
        if(!StringUtils.isEmpty(dto.getKeyWords())){
            queryWrapper.like(ScreenShouldKnowAnswer::getAnswer,dto.getKeyWords());
        }
        queryWrapper.orderByDesc(ScreenShouldKnowAnswer::getCreateTime);
        Page<ScreenShouldKnowAnswer> page = new Page<>(dto.getPageNum(),dto.getPageSize());
        Page<ScreenShouldKnowAnswer> standardPage = screenShouldKnowAnswerRepository.page(page, queryWrapper);
        return BaseResult.ok(standardPage,"查询成功");
    }
}
